package com.msmall.community.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

import com.msmall.base.util.Constants;
import com.msmall.base.util.StringUtil;
import com.msmall.community.common.Configuration;

public class WishUtil {
	public static final String PREFIX_WML = "file/wml/wish/type/";
	public static final String PREFIX_HTML = "file/html/wish/type/";
	
	/**
	 * 保存背景图片<br/>
	 * [0] 路径 file/html/wish/type/id/<br/>
	 * [1]名称 huangshan.jpg<br/>
	 * 首先删除 file/html/wish/type/id/oldfilename
	 */
	public static String[] saveBgImage(File file, String filename, String oldfilename, Long typeid, String uat){
		try {
			if(StringUtil.nullOrBlank(oldfilename)){
				File tFolder = new File(getAbsoluteHomePath(typeid, uat));
				if(!tFolder.exists()){
					tFolder.mkdirs();
				}
			}else{
				File oldFile = new File(getAbsoluteHomePath(typeid, uat) + oldfilename);
				if(oldFile.exists()){
					oldFile.delete();
				}else{
					File tFolder = new File(getAbsoluteHomePath(typeid, uat));
					if(!tFolder.exists()){
						tFolder.mkdirs();
					}
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			File target = new File(getAbsoluteHomePath(typeid, uat)+filename);
			IOUtils.copyLarge(new FileInputStream(file), new FileOutputStream(target));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new String[]{getHomePath(typeid, uat), filename};
	}
	
	/**
	 * 保存未激活图片<br/>
	 * [0] 路径 file/html/wish/type/id/<br/>
	 * [1]名称 huangshan.jpg<br/>
	 * 首先删除 file/html/wish/type/id/oldfilename
	 */
	public static String[] saveCommonImage(File file, String filename, String oldfilename, Long typeid, String uat){
		try {
			if(StringUtil.nullOrBlank(oldfilename)){
				File tFolder = new File(getAbsoluteHomePath(typeid, uat));
				if(!tFolder.exists()){
					tFolder.mkdirs();
				}
			}else{
				File oldFile = new File(getAbsoluteHomePath(typeid, uat) + oldfilename);
				if(oldFile.exists()){
					oldFile.delete();
				}else{
					File tFolder = new File(getAbsoluteHomePath(typeid, uat));
					if(!tFolder.exists()){
						tFolder.mkdirs();
					}
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			File target = new File(getAbsoluteHomePath(typeid, uat)+filename);
			IOUtils.copyLarge(new FileInputStream(file), new FileOutputStream(target));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new String[]{getHomePath(typeid, uat), filename};
	}
	/**
	 * 保存激活图片<br/>
	 * [0] 路径 file/html/wish/type/id/<br/>
	 * [1]名称 huangshan.jpg<br/>
	 * 首先删除 file/html/wish/type/id/oldfilename
	 */
	public static String[] saveActiveImage(File file, String filename, String oldfilename, Long typeid, String uat){
		try {
			if(StringUtil.nullOrBlank(oldfilename)){
				File tFolder = new File(getAbsoluteHomePath(typeid, uat));
				if(!tFolder.exists()){
					tFolder.mkdirs();
				}
			}else{
				File oldFile = new File(getAbsoluteHomePath(typeid, uat) + oldfilename);
				if(oldFile.exists()){
					oldFile.delete();
				}else{
					File tFolder = new File(getAbsoluteHomePath(typeid, uat));
					if(!tFolder.exists()){
						tFolder.mkdirs();
					}
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			File target = new File(getAbsoluteHomePath(typeid, uat)+filename);
			IOUtils.copyLarge(new FileInputStream(file), new FileOutputStream(target));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new String[]{getHomePath(typeid, uat), filename};
	}
	public static String getAbsoluteHomePath(Long typeid, String uat){
		return Constants.ROOTPATH + getHomePath(typeid, uat);
	}
	
	/**
	 * 生成图片路径 file/html/wish/type/id/
	 * @return
	 */
	public static String getHomePath(Long typeid, String uat){
		String path = "";
		if(uat.equals(Constants.USER_AGENT_TYPE_HTML)){
			path = PREFIX_HTML;
		}
		if(uat.equals(Constants.USER_AGENT_TYPE_WML)){
			path = PREFIX_WML;
		}
		path = path + typeid + "/";
		return path;
	}
	
	/**
	 * 获得WISH内容TIPS
	 */
	public static String getWishTips(String content){
		if(StringUtil.nullOrBlank(content)){
			return null;
		}
		if(content.length() > Configuration.getIntegerValue(Configuration.WISH_TIPS_LENGTH)){
			return content.substring(0, Configuration.getIntegerValue(Configuration.WISH_TIPS_LENGTH));
		}
		return content;
	}
}
